import { customAlphabet } from 'nanoid';
import crypto from 'crypto';
import { Dayjs, OpUnitType, QUnitType } from 'dayjs';

const RandomStr = customAlphabet('abcdefghijklmnpqrstuvwxyz0123456789QWERTYUIOPASDFGHJKLZXCVBNM', 6);

/**
 * 获取随机字符串
 * @param len 长度
 * @returns
 */
export function GetNonceStr(len?: number) {
  return RandomStr(len);
}

/**
 * 创建字符串的has1
 * @param str
 * @returns
 */
export function GetHas1(str: string) {
  const res = crypto.createHash('sha1');
  res.update(str, 'utf-8');
  return res.digest('hex');
}

/**
 * 获取当前时间的时间戳-秒
 * @returns
 */
export function GetTimeStamp() {
  return Math.round(Date.now() / 1000);
}

export function getMd5(str: string) {
  const md5 = crypto.createHash('md5');
  md5.update(str);
  return md5.digest('hex');
}

// 加密 aes-256-cbc
export function EncryptAesCBC(text: string, key: string) {
  const iv = key.substring(0, 16);
  const str = Buffer.from(text, 'base64').toString('binary');
  const decipher = crypto.createCipheriv('aes-256-cbc', key, iv);
  let decoded = decipher.update(str, 'binary', 'utf8');
  decoded += decipher.final('utf8');
  return decoded;
}

export function DecrpytAesCBC(crypted: string, key: string) {
  const iv = key.substring(0, 16);
  const str = Buffer.from(crypted, 'base64').toString('binary');
  const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
  let decoded = decipher.update(str, 'binary', 'utf8');
  decoded += decipher.final('utf8');
  return decoded;
}

export function getMobile(tell: string) {
  return tell.split('').reduce((call, code, index) => (index > 2 && index < 7 ? `${call}*` : call + code));
}

/**
 * 获取两个时间之前的差值, arg1 - arg2
 * @param arg1 第一个时间
 * @param arg2 第二个时间
 * @param unit 单位
 * @returns 差值
 */
export function getDiff(arg1: Dayjs, arg2: Dayjs, unit: QUnitType | OpUnitType = 'd') {
  if (!arg1) {
    return -1;
  }
  return arg1.diff(arg2, unit);
}

// 计算表格列宽
export const colWidth = (columns) => {
  const w = columns.reduce((count: number, cur) => count + (cur.width ? parseInt(cur.width, 10) : 0), 0);
  return w;
};

// api返回信息
export const backMessage = (data: string | null, msg?: string, code?: string) => ({
  code: code ?? '200',
  msg: msg ?? 'success',
  data,
});

// 首字母大写
export const titleCase = (str: string) => str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());

export const getQueryString = (name: string) => {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
  // var r = window.location.search.substr(1).match(reg);
  const r = decodeURIComponent(window.location.search.substr(1).replace('/+/g', '%20')).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};

// 日志文案字符串替换
export const replaceText = (data, str) => {
  if (!data.length) return str;
  const reg = /xx/g;
  let index = -1;
  let s = str;
  s = str.replace(reg, (_) => {
    index += 1;
    return data[index];
  });
  return s;
};

// 判断undefined和null
export const isEmpty = (value) => {
  if ((!value || typeof value === 'undefined') && value !== 0) {
    return true;
  }
  return false;
};
